package cn.uexpo.lhz.config;

import cn.uexpo.lhz.plus.R;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * 网关限制 直接控制路由id即可
 */

@Configuration
public class SentinelGateWayConfig {

    public SentinelGateWayConfig() {

        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {

            //网关限流了请求、就会调用此回调 Mono flus S   spring引入的响应式编程
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                R error = R.error(555555, "网关限流了请求");
                String s = JSON.toJSONString(error);
                Mono<ServerResponse> body = ServerResponse.ok().body(Mono.just(s), String.class);
                return body;
            }
        });
    }
}
